/**
 * Created by hubin on 2017/7/17.
 */

// * * * * * *
// ┬ ┬ ┬ ┬ ┬ ┬
// │ │ │ │ │ |
// │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
// │ │ │ │ └───── month (1 - 12)
// │ │ │ └────────── day of month (1 - 31)
// │ │ └─────────────── hour (0 - 23)
// │ └──────────────────── minute (0 - 59)
// └───────────────────────── second (0 - 59, OPTIONAL)
let schedule = require('node-schedule');
let {Contact} = require('wechaty');
let get_weather = require('./weather');

function scheduleCronstyle(){
    let rule = {hour: [8,19], minute: 0};
    schedule.scheduleJob(rule, function(){
        let city = '北京';
        let date = new Date();
        let hour = date.getHours();// 0-23
        let week = date.getDay();// 0-6,0代表星期天
        if(0 < week < 6){// 工作日
            if(hour <= 10 || hour >= 19){// 在家
                city = '昌平区';
            }else if(hour < 19){// 在公司
                city = '北京';
            }
        }else{// 周末
            city = '昌平区';
        }
        get_weather(city, function (error, response, body) {
            //console.log(info);
            if(error){
                console.error('查询天气出错：'+error.message);
            }
            var replyStr = '没查询到天气吆！自己去查询吧：http://beijing.tianqi.com/';
            if (!error && response.statusCode === 200) {
                let info = JSON.parse(body);
                if(info.status === '0'){
                    replyStr =
                        "日期：" + info.result.date + " " + info.result.week + "\n"
                        + "城市：" + info.result.city + "\n"
                        + "天气：" + info.result.weather + "\n"
                        + "气温：" + info.result.templow + "~" + info.result.temphigh + "\n"
                        + "风力：" + info.result.winddirect + info.result.windpower + "\n"
                        + info.result.updatetime + "\n"
                        + "更多天气：http://beijing.tianqi.com/";
                }
            }
            Contact.find({alias: "feifei"})
                .then(contact => {
                    contact.say(replyStr)
                    return 'OK';
                });
            Contact.find({alias: "chrome"})
                .then(contact => {
                    contact.say(replyStr)
                    return 'OK';
                });
        });
    });
}

module.exports = scheduleCronstyle;